import { z } from 'zod';
import { NodeType } from '../types/behaviorTree';

/**
 * 层级化节点的 Zod Schema
 * 用于验证导入的 JSON 数据结构
 */
export const HierarchicalNodeSchema: z.ZodType<unknown> = z.lazy(() =>
  z.object({
    id: z.string().min(1, '节点 ID 不能为空'),
    type: z.enum([
      NodeType.Sequence as NodeType.Sequence,
      NodeType.Selector as NodeType.Selector,
      NodeType.Action as NodeType.Action,
      NodeType.Condition as NodeType.Condition,
      NodeType.Decorator as NodeType.Decorator,
    ]),
    name: z.string().min(1, '节点名称不能为空'),
    properties: z.record(z.string(), z.unknown()).optional(),
    children: z.array(HierarchicalNodeSchema).optional(),
  })
);

/**
 * 验证层级化 JSON 数据
 * @param data 待验证的数据
 * @returns 验证后的数据
 * @throws ZodError 如果验证失败
 */
export function validateHierarchyJSON(data: unknown) {
  return HierarchicalNodeSchema.parse(data);
}

